package com.tuojiahr.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.global.Constants;
import com.tuojiahr.tools.FileUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-15 上午12:17:51
 */

public class ImgSetAction extends BaseActionCommon {

	private static final long serialVersionUID = 1L;

	private String url = "adminmgr/img_openHeaderImgSetView";
	private String message = "出错啦！！！";
	
	private File imgFile;
	
	/**
	 * 打开首页header图片设置
	 * @return
	 */
	public String openHeaderImgSetView() {
		if(null != getCurrUser()) {
			return "header_img_set";
		}
		return "msg";
	}

	/**
	 * 更新首页header图片
	 * @throws IOException 
	 */
	public void updateHeaderImg() throws IOException {
		String result = "-2";
		double maxFileSize = 1024 * 1024 * 2; 
		if(null != getCurrUser() && null  != imgFile){
			//开始复制文件
			FileInputStream fis = new FileInputStream(imgFile);
			double fileSize =  (double) fis.available();
			if(maxFileSize > fileSize) {
				try {
					 FileUtil.fileToImg(imgFile, null, "top.jpg");
					 result = "2";
				} catch (Exception e) {
					Constants.ExceptionLog.error("复制文件异常：", e);
				}
			} else {
				//图片大于2M
				result = "-5";
			}
		} 
		
		response.getWriter().write(result);
		response.getWriter().close();
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}
}
